<template>
  <div>
    <h1 style="text-align: center">分类添加</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="分类名称">
        <el-input v-model="form.catname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onclick">立即新增</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入新增分类 函数
import { CatAdd } from "@/request";

export default {
  data() {
    return {
      form: {
        catname: "",
      },
    };
  },
  methods: {
    //发送请求新增分类
    async onclick() {
      //获取form表单中的数据
      let data = await CatAdd(this.form);
      // console.log(data);
      //分类添加成功或者失败 响应给用户
      let { error_code, msg } = data;
      //成功
      if (error_code === 200) {
        this.$message({
          type: "succcess",
          message: msg,
          duration: 1500,
          //添加成跳转 分类列表
          onClose: () => {
            this.$router.push("/cat/list"),
              (this.form = {
                form: {
                  catname: "",
                },
              });
          },
        });
      } else {
        //添加失败
        this.$$message({
          type: "error",
          message: msg,
          duration: 1500,
        });
      }
    },
  },
};
</script>

<style>
</style>
